function [output_list, select_flag] = guiradio(fig_title, radio_title, input_list, select_type, radio_layout);

% GUIRADIO - create an array of labeled radio buttons
%
%    [OUTPUT, FLAG] = GUIRADIO(FTITLE, BTITLE, INPUT, 'multiple') creates
%    a figure named FTITLE containing an array of radio buttons labeled
%    with the titles contained in the cell array BTITLE.  The indices of
%    the radio buttons to be initially checked are provided in the array
%    INPUT.  The indices of the buttons checked when the figure is exited
%    is returned as OUTPUT, and FLAG indicates how the figure was exited
%    (0 if Cancel, 1 if OK).
%
%    [...] = GUIRADIO(FTITLE, BTITLE, INPUT, 'single') allows one radio
%    radio to be checked at a time.  INPUT must be a scalar to indicate
%    the index of the radio that is checked when the figure is created.
%
%    [...] = GUIRADIO(FTITLE, ..., LAYOUT) specifies the layout of the
%    radio buttons, where LAYOUT is a two element vector containing the
%    number of rows & columns of radio buttons.

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: May 18, 2000

% check input parameters

if ((nargin < 4) | (nargin > 5))
   return
elseif (~ischar(fig_title))
   return
elseif (~iscell(radio_title))
   return
elseif (~isnumeric(input_list))
   return
elseif (~ischar(select_type))
   return
elseif (nargin == 4)
   radio_layout = [];
elseif (~isnumeric(radio_layout))
   return
end

% calculate layout of radio buttons
% make sure no more than ten-fold difference between rows & columns

num_radio = length(radio_title);
if (length(radio_layout) == 2)
   num_rows = radio_layout(1);
   num_cols = radio_layout(2);
   while (abs(log10(num_rows/num_cols)) >= 1)
      if (num_rows > num_cols)
         num_rows = ceil(num_rows/2);
         num_cols = 2*num_cols;
      else
         num_rows = 2*num_rows;
         num_cols = ceil(num_cols/2);
      end
   end
else
   num_rows = floor(sqrt(num_radio));
   num_cols = ceil(num_radio/num_rows);
end

% only allow valid radio indices in input list
% make sure list is scalar for single selection

valid_list = input_list(find((input_list >= 1) & (input_list <= num_radio)));
if (strcmp(select_type, 'single') & (length(valid_list) ~= 1))
   return
end

% calculate size & location of figure window

fig_height = min(0.8, (0.1 + 0.035*num_rows));
fig_width = min(0.8, (0.05 + 0.075*num_cols));
fig_pos = [(1 - fig_width)/2 (1 - fig_height)/2 fig_width fig_height];

% calculate size & spacing of radio buttons
% horizontal space = radio radio width / 4
% vertical space = radio radio height / 2

radio_width = 4/(5*num_cols + 1);
space_width = radio_width/4;
radio_height = 2/(3*num_rows + 6);
space_height = radio_height/2;
button_height = 3*radio_height/2;

% create a new figure window to hold the radio buttons

h_fig = figure('Color', [0.8 0.8 0.8], ...
   'Units', 'normalized', ...
   'Position', fig_pos, ...
   'MenuBar', 'none', ...
   'Name', fig_title, ...
   'NumberTitle', 'off', ...
   'CloseRequestFcn', 'set(gcf, ''UserData'', 0)');

% create array of radio buttons
% layout in rows & columns

h_radio = zeros(1, num_radio);
y_pos = 1;
current_radio = 0;
for i = 1 : num_rows
   x_pos = space_width;
   y_pos = y_pos - radio_height - space_height;
   
   % if not enough buttons in row, adjust horizontal spacing
   
   num_remain = num_radio - current_radio;
   if (num_remain < num_cols)
      x_pos = x_pos + (num_cols - num_remain) * (radio_width + space_width)/2;
   end
   
   % create radio buttons
   % initially set as unchecked
   
   for j = 1 : num_cols
      current_radio = current_radio + 1;
      if (current_radio > num_radio)
         break
      end
      h_radio(current_radio) = uicontrol('Parent', h_fig, ...
         'Style', 'radiobutton', ...
         'Units', 'normalized', ...
         'Position', [x_pos y_pos radio_width radio_height], ...
         'BackgroundColor', [0.8 0.8 0.8], ...
         'ForegroundColor', [0 0 0], ...
         'Min', 0, 'Max', 1, ...
         'String', radio_title{current_radio}, ...
         'Value', 0);
      x_pos = x_pos + radio_width + space_width;
   end
end

% create callback for single select type

if (strcmp(select_type, 'single'))
   for i = 1 : num_radio
      set(h_radio(i), 'Callback', [...
            'h_temp = findobj(get(gcbo, ''Parent''), ''Type'', ''uicontrol'', ''Style'', ''radiobutton''); ' ...
            'for i = 1 : length(h_temp) set(h_temp(i), ''Value'', 0); end; ' ...
            'set(gcbo, ''Value'', 1); clear h_temp;']);
   end
end
        
% select specified radio buttons

for i = valid_list
   set(h_radio(i), 'Value', 1);
end

% create separator
% create control buttons

y_pos = y_pos - space_height;
h_frame = uicontrol('Parent', h_fig, ...
   'Style', 'frame', ...
   'Units', 'normalized', ...
   'Position', [0 y_pos 1 button_height/20]);

y_pos = y_pos - button_height - space_height;
button_pos = [0.1 y_pos 0.8 button_height];
if (strcmp(select_type, 'multiple'))
   button_string = {'OK', 'All', 'None', 'Cancel'};
   button_callback = {'set(gcf, ''UserData'', 1)', ...
         'set(findobj(gcf, ''Type'', ''uicontrol'', ''Style'', ''radio''), ''Value'', 1)', ...
         'set(findobj(gcf, ''Type'', ''uicontrol'', ''Style'', ''radio''), ''Value'', 0)', ...
         'set(gcf, ''UserData'', 0)'};
else
   button_string = {'OK', 'Cancel'};
   button_callback = {'set(gcf, ''UserData'', 1)', ...
         'set(gcf, ''UserData'', 0)'};
end
h_button = guipanel(h_fig, button_pos, 'horizontal', 'String', button_string, 'Callback', button_callback);

% wait for user to click radio or close edit figure
% copy array values if selected

waitfor(h_fig, 'UserData');
select_flag = get(h_fig, 'UserData');
output_list = [];
if (select_flag)
   for i = 1 : num_radio
      if (get(h_radio(i), 'Value'))
         output_list = [output_list i];
      end
   end
end

% delete edit figure

if (ishandle(h_fig))
   delete(h_fig);
end
return
